package com.happystreet.config
{
	import com.happystreet.GlobalGameConstants;
	import com.happystreet.engine.CoreShop;
	import com.happystreet.info.FriendDeliveryInfo;
	import com.happystreet.info.UserInfo;
	import com.happystreet.manager.CollectionsManager;
	import com.happystreet.manager.TutorialManager;
	import com.happystreet.utils.CustomUtils;
	import com.happystreet.utils.DataUtils;

	/**
	 *游戏配置 
	 * @author Liseen
	 * 
	 */	
	public class GameConfig
	{
		
		//最大背包配置
		public static const PACKS_CONFIG:Array = [3, 4, 5, 6, 7, 8];
		//交付背包配置
		public static const DELIVERY_PACKS_CONFIG:Array = [2, 4, 6, 9, 12, 16, 16, 16, 16, 16, 16, 16];
		//超级货车价格
		public static const SUPERTRUCKS_PRICES:Array = [5, 10, 15, 30, 50, 80];
		public static const SUPERTRUCKS_COEFFS:Array = [2, 4, 6, 8, 12, 20];
		//商店最大等级
		public static const SHOP_MAX_LEVEL:int = 5;
		
		public static const SHOP_WIDTH_LVL0:int = 150;
		public static const SHOP_WIDTH_LVL1:int = 220;
		public static const SHOP_WIDTH_LVL2:int = 260;
		public static const SHOP_WIDTH_LVL3:int = 300;
		//商店商店占地宽度
		public static const SHOP_AREA_WIDTH:int = 500;
		//商店间隔
		public static const SHOP_AREA_INTERVAL:int = 0;
		//商店占地范围
		public static const SHOP_AREA_GROUND:int = 433;
		//房顶基础宽度
		public static const SHOP_ROOF_BASE_WIDTH:int = 370;
		//房顶基础高度
		public static const SHOP_ROOF_BASE_HEIGHT:int = 130;
		//商店墙壁高度
		public static const SHOP_WALL_HEIGHT:int = 86;
		
		public static const HIT_AREA_ALPHA:Number = 0;
		//墙壁砖块大小
		public static const SHOP_WALL_TILE_SIZE:int = 43;
		//拍照缩放系数
		public static const CAMERA_SHOP_VIEW_ZOOM_FACTOR:Number = 1.2;
		//街道水平高
		public static const STREET_HORIZON_HEIGHT:int = 300;
		//服务器地址
		public static var bridgeUrl:String;
		//用户数据信息对象
		public static var userInfo:UserInfo;
		//游戏版本
		public static var gameVersion:String;
		//服务器时间戳
		public static var serverTimestamp:Number;
		//游戏基础路径
		public static var gameBasePath:String;
		//正在使用的的背景
		public static var workingUserBackground:int;
		//出售剩余秒数
		public static var salesSecondsLeft:Number;
		//商店数据
		public static var shopData:Array;
		//街道数据
		public static var streetData:Object;
		
		public static var maxDeliveryPackConfig:Array;
		public static var supertrucksPrices:Array;
		public static var supertrucksCoeffs:Array;
		public static var rushMeterMaxList:Array;
		//新手指导
		private static var tuto:Object;
		
		//UpgradeInfo
		public static const COINS_CURRENCY:String = "coins";
		public static const CASH_CURRENCY:String = "cash";
		//用户语言
		public static var userLanguage:String;
		//商店列表
		public static var shopList:Array = new Array();
		public static var userShopList:Array = new Array();
		public static var nbShops:int;
		public static var streetLength:int;
		//之前的游戏模式
		public static var previousGameMode:String;
		//当前的游戏模式
		public static var gameMode:String;
		//选择的商店的ID
		public static var selectedShopId:int;
		public function GameConfig()
		{
			
		}
		/**
		 *设置用户数据 
		 * @param data	服务器响应数据
		 * 
		 */		
		public static function setUserGameData(data:Object):void
		{
			userInfo.user_level = int(data.user_level);
			userInfo.user_xp = int(data.user_xp);
			userInfo.xp_next = int(data.xp_next);
			userInfo.xp_previous = int(data.xp_previous);
			userInfo.street = data.street;
			userInfo.user_coins = uint(data.user_coins);
			userInfo.user_cash = int(data.user_cash);
			userInfo.max_shops = int(data.max_shops);
			userInfo.background = data.background ? data.background : GlobalGameConstants.DEFAULT_STREET_BACKGROUND;
			workingUserBackground = userInfo.background;
			userInfo.jobsXPList = (data.jobs_list!=null) ? data.jobs_list : new Object();
			userInfo.jobsReceived = (data.jobs_received!=null) ? data.jobs_received : new Object();
			userInfo.maxPackConfig = PACKS_CONFIG;
			userInfo.warehouse = data.warehouse;
			userInfo.parts = data.inventory ? DataUtils.formatObjectDataToArray(data.inventory.parts) : [];
			userInfo.giftsReceived = data.gifts_received;
			userInfo.customer_data = data.customer_data;
			CustomersConfig.oldCustomer_data = userInfo.customer_data;
			userInfo.achievements = data.achievements ? data.achievements : {};
			userInfo.hasStreet = true;
			userInfo.rank = (data.user_rank!=null) ? data.user_rank : "Default";
			userInfo.is_vip = (data.is_vip!=null) ? data.is_vip : 0;
			userInfo.multiLoveLevel = 0;
			userInfo.multiLoveMaxLevel = 0;
			userInfo.love = data.user_love;
			userInfo.energy = int(data.user_energy);
			userInfo.maxLove = data.user_love_max;
			userInfo.nextLoveDelay = data.user_love_more_in;
			userInfo.defaultLoveDelay = data.user_love_more_in_max;
			userInfo.maxEnergy = data.user_energy_max;
			userInfo.nextEnergyDelay = data.user_energy_more_in;
			userInfo.defaultEnergyDelay = data.user_energy_more_in_max;
			userInfo.pendingDelightCount = 0;
			//userInfo.missionTable = MissionsConfig.getUpdatedMissionsFromInitResponse(data.missions);
			userInfo.canUnlockLoveMission = (data.hasOwnProperty("canUnlockLoveMission")) ? data.canUnlockLoveMission : false;
			userInfo.todayReport = data.user_today_report;
			userInfo.completedMissionIds = data.missions_completed;
			userInfo.isEligibleForMission19 = (data.is_eligible_for_mission_19!=null) ? data.is_eligible_for_mission_19 : 0;
			salesSecondsLeft = int(data.sales_seconds_left);
			initSalesTimer();
			if (data.hasOwnProperty("collectables"))
			{
				CollectionsManager.isEnabled = true;
				userInfo.collectables = data.collectables;
				userInfo.completed_collections = (!(data.completed_collections == null)) && data.hasOwnProperty("completed_collections") ? data.completed_collections : [];
				userInfo.wishlist = (data.hasOwnProperty("wishlist")) ? CollectionsManager.checkWishList(data.wishlist) : [];
			}
			else
			{
				CollectionsManager.isEnabled = false;
				userInfo.collectables = {};
				userInfo.wishlist = [];
				userInfo.completed_collections = [];
			}
			shopData = data.shop_data;
			streetData = DataUtils.checkStreetData(data.street);
			maxDeliveryPackConfig = DELIVERY_PACKS_CONFIG;
			supertrucksPrices = SUPERTRUCKS_PRICES;
			supertrucksCoeffs = SUPERTRUCKS_COEFFS;
			userInfo.rushMeters = getRushFromShopData(shopData);
			rushMeterMaxList = new Array();
			if (data.hasOwnProperty("tutorial_step"))
			{
				tuto = TutorialManager.getInstance();
				tuto.initTutorial();
				userInfo.tutorialStep = Math.max(data.tutorial_step, 1);
			}
			userInfo.friendDeliveries = getFriendDeliveriesFromShopData(data.shop_data);
			userInfo.hasSentDeliveries = data.has_sent_deliveries;
			userInfo.hasSpunToday = CustomUtils.booleanFromXmlString(data.has_spun_today, true);
			GoodsConfig.setGoodsLevelsFromShopData(shopData);
			userInfo.askForMorePartDelay = data.reset_ask_part;
			setAskPartTimer(userInfo.askForMorePartDelay);
			userInfo.askForMoreMaterialDelay = data.reset_ask_material;
			setAskMaterialTimer(userInfo.askForMoreMaterialDelay);
			userInfo.materials = data.inventory.hasOwnProperty("materials") ? data.inventory.materials : new Object();
			
		}
		
		public static function getShopIdByType(_arg1:int):int
		{
			var _local2:int;
			var _local3:Array = new Array();
			var _local4:int;
			while (_local4 < streetData.length)
			{
				if (streetData[_local4].type == _arg1)
				{
					_local3.push(_local4);
				}
				_local4++;
			}
			if (_local3.length == 1)
			{
				_local2 = _local3[0];
			}
			else
			{
				if (_local3.length > 1)
				{
					_local2 = _local3[Math.floor((Math.random() * _local3.length))];
				}
				else
				{
					_local2 = -1;
				}
			}
			return _local2;
		}
		
		
		/**
		 *是否有在售的商品 
		 * @return 
		 * 
		 */		
		public static function areThereSales():Boolean
		{
			if (salesSecondsLeft > 0)
			{
				return true;
			}
			return false;
		}
		/**
		 *获取默认的墙壁数据 
		 * @return 
		 * 
		 */		
		public static function getDefaultWallData():Array
		{
			var _local1:Array = [3, GlobalGameConstants.DEFAULT_WALL_SHAPE, GlobalGameConstants.DEFAULT_WALL_MATERIAL];
			return _local1;
		}
		
		/**
		 *获取默认的屋顶数据 
		 * @return 
		 * 
		 */		
		public static function getDefaultRoofData():Array
		{
			var list:Array = [5, 425000];
			return list;
		}
		
		public static function setAskPartTimer(value:int):void
		{
			
		}
		
		public static function setAskMaterialTimer(value:int):void
		{
			
		}
		/**
		 *添加商店 
		 * @param shop
		 * 
		 */		
		public static function addShop(shop:CoreShop):void
		{
			shopList.push(shop);
			if (gameMode != GlobalGameConstants.VISIT_STREET_GAMEMODE)
			{
				userShopList.push(shop);
			}
			nbShops = shopList.length;
			streetLength = (nbShops - 1) * (SHOP_AREA_WIDTH + SHOP_AREA_INTERVAL);
			var friendDeliveryInfo:FriendDeliveryInfo = new FriendDeliveryInfo();
			userInfo.friendDeliveries.push(friendDeliveryInfo);
		}
		
		/**
		 *获取商店布局 
		 * @param level
		 * @return 
		 * 
		 */		
		public static function getShopLayout(level:int):Array
		{
			var list:Array = new Array();
			switch (Math.min(level, SHOP_MAX_LEVEL))
			{
				default:
					list = [SHOP_WIDTH_LVL0, 1];
					break;
				case 1:
					list = [SHOP_WIDTH_LVL1, 1];
					break;
				case 2:
					list = [SHOP_WIDTH_LVL1, 2];
					break;
				case 3:
					list = [SHOP_WIDTH_LVL2, 2];
					break;
				case 4:
					list = [SHOP_WIDTH_LVL2, 3];
					break;
				case SHOP_MAX_LEVEL:
					list = [SHOP_WIDTH_LVL3, 3];
			}
			return list;
		}
		
		/**
		 *初始化销售计时器 
		 * 
		 */		
		private static function initSalesTimer():void
		{
			if(!areThereSales())
			{
				trace("** There are no sales");
				return;
			}
			
			
		}
		
		/**
		 *获取加速 
		 * @param value
		 * @return 
		 * 
		 */		
		private static function getRushFromShopData(value:Object):Array
		{
			var data:Object;
			var dataList:Array = new Array();
			for each (data in value)
			{
				if (data.hasOwnProperty("rush"))
				{
					dataList.push(data.rush);
				}
				else
				{
					dataList.push(0);
				}
			}
			return dataList;
		}
		
		/**
		 *获取好友之间贸易数据 
		 * @return 
		 * 
		 */		
		private static function getFriendDeliveriesFromShopData(value:Object):Array
		{
			return [];
		}
		
		
	}
}